#include "lcmusic.h"

LV_IMG_DECLARE(img_benchmark_cogwheel_argb);
LV_FONT_DECLARE(arial_18);

static lv_obj_t * music_bar;
static lv_obj_t * music_list;
static lv_obj_t * pause_label;

#define LC_MUSIC_PLAY_SYBOL LV_SYMBOL_PAUSE
#define LC_MUSIC_PAUSE_SYBOL LV_SYMBOL_PLAY

static void event_handler(lv_event_t * e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    int index = (int)(intptr_t)lv_obj_get_user_data(btn);
    lcmusic_app_data_t* lcmusic = lcmusic_get_data();
    printf("Button %d clicked\n", index);
    if (code == LV_EVENT_CLICKED) {
        lcmusic_play_index(lcmusic, index);

        lv_obj_clean(music_list);
        lv_obj_add_flag(music_list, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(music_bar, LV_OBJ_FLAG_HIDDEN);
        
        lv_label_set_text(pause_label, LC_MUSIC_PLAY_SYBOL);
    }
}

void create_music_list(void)
{
    /*Create a list*/
    lv_obj_t * list = lv_list_create(music_list);
    lv_obj_set_size(list, lv_pct(100), lv_pct(100));
    lv_obj_set_align(list, LV_ALIGN_TOP_MID);
    lv_obj_set_style_border_side(list, LV_BORDER_SIDE_TOP,0); 
    
    static lv_style_t list_label_style;
    lv_style_init(&list_label_style);
    lv_style_set_text_align(&list_label_style, LV_TEXT_ALIGN_LEFT);
    lv_style_set_text_font(&list_label_style, &arial_18);

    lcmusic_app_data_t* lcmusic = lcmusic_get_data();

    /*Add buttons to the list*/
    lv_obj_t * btn;
    for(int i = 0; i < lcmusic->sum_track_no; i++) {
        btn = lv_list_add_btn(list, LV_SYMBOL_AUDIO, lcmusic->file_iterator->list[i]);
        lv_obj_set_width(btn, lv_pct(100));
        lv_obj_set_height(btn, lv_pct(25));
        lv_obj_set_flex_flow(btn, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(btn, 
                            LV_FLEX_ALIGN_SPACE_BETWEEN,
                            LV_FLEX_ALIGN_CENTER,
                            LV_FLEX_ALIGN_CENTER);
        lv_obj_set_user_data(btn, (void*)(intptr_t)i);

        // lv_obj_t* icon = lv_obj_get_child(btn, 0);

        lv_obj_t * label = lv_obj_get_child(btn, 1); 
        lv_obj_add_style(label, &list_label_style, 0);
        lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
    }
}

void lcmusic_ui_back_btn_event_cb(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    
    if (code == LV_EVENT_CLICKED)
    {
        printf("back button clicked\n");
        lv_obj_clean(music_list);
        lv_obj_add_flag(music_list, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(music_bar, LV_OBJ_FLAG_HIDDEN);
        lv_label_set_text(pause_label, LC_MUSIC_PLAY_SYBOL);
        sys_app_quit(sys_get_cur_app());
    }
}

void lcmusic_ui_list_btn_event_cb(lv_event_t *event)
{
    static uint8_t list_flag = 0;
    lv_event_code_t code = lv_event_get_code(event);
    if (code == LV_EVENT_CLICKED)
    {
        printf("list button clicked\n");
        if(list_flag == 0)
        {
            create_music_list();
            lv_obj_add_flag(music_bar, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(music_list, LV_OBJ_FLAG_HIDDEN);
        }else {
            lv_obj_clean(music_list);
            lv_obj_add_flag(music_list, LV_OBJ_FLAG_HIDDEN);
            lv_obj_clear_flag(music_bar, LV_OBJ_FLAG_HIDDEN);
        }
        list_flag = ~list_flag;
    }
}

void lcmusic_ui_pause_btn_event_cb(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    lv_obj_t * label = lv_event_get_user_data(event);
    const char * text = lv_label_get_text(label);
    lcmusic_app_data_t* lcmusic = lcmusic_get_data();

    if (code == LV_EVENT_CLICKED)
    {
        printf("text: %s\n", text);
        if (strcmp(text, LC_MUSIC_PLAY_SYBOL) == 0) {
            lcmusic_stop_play(lcmusic);
            lv_label_set_text(label, LC_MUSIC_PAUSE_SYBOL);
        } else if (strcmp(text, LC_MUSIC_PAUSE_SYBOL) == 0) {
            lcmusic_start_play(lcmusic);
            lv_label_set_text(label, LC_MUSIC_PLAY_SYBOL);
        }
    }
}

void lcmusic_ui_next_btn_event_cb(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    lcmusic_app_data_t* lcmusic = lcmusic_get_data();

    if (code == LV_EVENT_CLICKED)
    {
        lcmusic_play_next(lcmusic);
    }
}

void lcmusic_ui_prev_btn_event_cb(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    lcmusic_app_data_t* lcmusic = lcmusic_get_data();

    if (code == LV_EVENT_CLICKED)
    {
        lcmusic_play_prev(lcmusic);
    }
}

lv_obj_t* lcmusic_lvgl_register(void)
{
    lv_obj_t* lv_obj = lv_obj_create(NULL);
    if (lv_obj == NULL) {
        LV_LOG_ERROR("Failed to create main music app object.");
        return NULL;
    }
    lv_obj_clear_flag(lv_obj, LV_OBJ_FLAG_SCROLLABLE);

    static lv_style_t style_btn;
    lv_style_init(&style_btn);
    lv_style_set_bg_opa(&style_btn, 0);
    lv_style_set_pad_all(&style_btn, 0);
    lv_style_set_shadow_opa(&style_btn, 0);


    lv_obj_t * nav_bar = lv_obj_create(lv_obj);
    lv_obj_set_style_border_width(nav_bar, 0, LV_PART_MAIN);
    lv_obj_set_size(nav_bar, LV_HOR_RES, LV_VER_RES / 5);
    lv_obj_align(nav_bar, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_set_style_radius(nav_bar, 0, 0);
    lv_obj_set_style_bg_color(nav_bar, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_pad_all(nav_bar, 0, LV_PART_MAIN);
    lv_obj_clear_flag(nav_bar, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_t * back_btn = lv_btn_create(nav_bar);
    lv_obj_set_size(back_btn, 100, 40);
    lv_obj_add_style(back_btn, &style_btn, 0);
    lv_obj_align(back_btn, LV_ALIGN_LEFT_MID, 0, 0);  
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_obj_align(back_label, LV_ALIGN_LEFT_MID, 10, 0);
    lv_label_set_text(back_label, LV_SYMBOL_LEFT " back");
    lv_obj_set_style_text_color(back_label, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(back_label, &lv_font_montserrat_24, 0);
    lv_obj_add_event_cb(back_btn, lcmusic_ui_back_btn_event_cb, LV_EVENT_CLICKED, lv_obj);

    lv_obj_t * list_btn = lv_btn_create(nav_bar);
    lv_obj_set_size(list_btn, 50, 40);
    lv_obj_add_style(list_btn, &style_btn, 0);
    lv_obj_align(list_btn, LV_ALIGN_RIGHT_MID, 0, 0);
    lv_obj_t * list_label = lv_label_create(list_btn);
    lv_obj_align(list_label, LV_ALIGN_RIGHT_MID, -10, 0);
    lv_label_set_text(list_label, LV_SYMBOL_LIST);
    lv_obj_set_style_text_color(list_label, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(list_label, &lv_font_montserrat_24, 0);
    lv_obj_add_event_cb(list_btn, lcmusic_ui_list_btn_event_cb, LV_EVENT_CLICKED, NULL);

    music_bar = lv_obj_create(lv_obj);
    lv_obj_set_style_border_width(music_bar, 0, LV_PART_MAIN);
    lv_obj_set_size(music_bar, LV_HOR_RES, LV_VER_RES * 4 / 5);
    lv_obj_align(music_bar, LV_ALIGN_TOP_LEFT, 0, LV_VER_RES / 5);
    lv_obj_set_style_radius(music_bar, 0, 0);
    lv_obj_set_style_bg_color(music_bar, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_pad_all(music_bar, 0, LV_PART_MAIN);   
    lv_obj_clear_flag(music_bar, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_t * music_img = lv_img_create(music_bar);
    lv_img_set_src(music_img, &img_benchmark_cogwheel_argb);
    lv_obj_align(music_img, LV_ALIGN_TOP_MID, 0, 10);

    lv_obj_t * pre_btn = lv_btn_create(music_bar);
    lv_obj_set_size(pre_btn, 60, 60);
    lv_obj_add_style(pre_btn, &style_btn, 0);
    lv_obj_align(pre_btn, LV_ALIGN_BOTTOM_LEFT, 40, 0);
    lv_obj_t * pre_label = lv_label_create(pre_btn);
    lv_obj_align(pre_label, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(pre_label, LV_SYMBOL_PREV);
    lv_obj_set_style_text_color(pre_label, lv_color_hex(0x707070), 0);
    lv_obj_set_style_text_font(pre_label, &lv_font_montserrat_32, 0);
    lv_obj_add_event_cb(pre_btn, lcmusic_ui_prev_btn_event_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t * pause_btn = lv_btn_create(music_bar);
    lv_obj_set_size(pause_btn, 60, 60);
    lv_obj_add_style(pause_btn, &style_btn, 0);
    lv_obj_align(pause_btn, LV_ALIGN_BOTTOM_MID, 0, 0);
    pause_label = lv_label_create(pause_btn);
    lv_obj_align(pause_label, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(pause_label, LC_MUSIC_PLAY_SYBOL);
    lv_obj_set_style_text_color(pause_label, lv_color_hex(0x707070), 0);
    lv_obj_set_style_text_font(pause_label, &lv_font_montserrat_32, 0);
    lv_obj_add_event_cb(pause_btn, lcmusic_ui_pause_btn_event_cb, LV_EVENT_CLICKED, pause_label);

    lv_obj_t * next_btn = lv_btn_create(music_bar);
    lv_obj_set_size(next_btn, 60, 60);
    lv_obj_add_style(next_btn, &style_btn, 0);
    lv_obj_align(next_btn, LV_ALIGN_BOTTOM_RIGHT, -40, 0);
    lv_obj_t * next_label = lv_label_create(next_btn);
    lv_obj_align(next_label, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(next_label, LV_SYMBOL_NEXT);
    lv_obj_set_style_text_color(next_label, lv_color_hex(0x707070), 0);
    lv_obj_set_style_text_font(next_label, &lv_font_montserrat_32, 0);
    lv_obj_add_event_cb(next_btn, lcmusic_ui_next_btn_event_cb, LV_EVENT_CLICKED, NULL);

    /*music list*/
    music_list = lv_obj_create(lv_obj);
    lv_obj_set_style_border_width(music_list, 0, LV_PART_MAIN);
    lv_obj_set_size(music_list, LV_HOR_RES, LV_VER_RES * 4 / 5);
    lv_obj_align(music_list, LV_ALIGN_TOP_LEFT, 0, LV_VER_RES / 5);
    lv_obj_set_style_radius(music_list, 0, 0);
    lv_obj_set_style_bg_color(music_list, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_pad_all(music_list, 0, LV_PART_MAIN);   
    lv_obj_clear_flag(music_list, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_add_flag(music_list,  LV_OBJ_FLAG_HIDDEN);


    return lv_obj;
}

static const ui_key_map_t common_keymap[] = {
    { KEY_RESERVED,     0,  0,  0 },
};

static int lcmusic_view_proc(uint8_t view_id, uint8_t msg_id, uint32_t ui_event)
{
	ESP_LOGI(TAG, "view_id %d msg_id %d ui_event %lu\n", view_id, msg_id, ui_event);

	switch (msg_id) {
        case MSG_VIEW_CREATE:
            ui_manager_load_acr_anim(LCMUSIC_VIEW, LV_SCR_LOAD_ANIM_MOVE_LEFT, 200, 0, false);
            lv_label_set_text(pause_label, LC_MUSIC_PLAY_SYBOL);
        break;

        case MSG_VIEW_DELETE:
            ui_manager_load_acr_anim(MENU_VIEW, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 200, 0, false);
        break;
	
        default:
        break;
	}
	return 0;
}

void lcmusic_view_init(void)
{
    ui_view_info_t view_info;

	memset(&view_info, 0, sizeof(ui_view_info_t));

	view_info.view_proc = lcmusic_view_proc;
	view_info.view_key_map = common_keymap;
	view_info.view_get_state = NULL;
	view_info.order = 1;
	view_info.task_name = APP_TASK_NAME;

	ui_view_create(LCMUSIC_VIEW, &view_info);
}

void lcmusic_view_exit(void)
{
	ui_view_delete(LCMUSIC_VIEW);
}